home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 2 / BBS in a box - Trilogy II.iso / Files / Hyper / H-Hx / Handling Apple events in HC 2.1 < prev    next >
Encoding:
Text File  |  1991-06-27  |  5.4 KB  |  79 lines  |  [TEXT/MSWD]

  1. Handling Apple events in HyperCard 2.1
  2. by Kevin Calhoun
  3.  
  4. When HyperCard 2.1 receives an Apple event, it sends an appleEvent message to the current card.  That means that you can respond to Apple events with HyperTalk scripts.  But before you try to write such scripts, there are some terms and conventions you should be familiar with.
  5.  
  6. The figure
  7. Apple events are like a package full of raw materials that arrive at a factory.  They have a return address on them -- in HyperTalk, a string of the form "Zone:Macintosh:Program".  They have an explanation of what their contents are for -- the class and id, which together consititute a verb, directing the receiver what do to with the package.  And of course there are the contents themselves -- the event parameters.
  8.  
  9. When you get one of these packages, you have to unpack it before you can make something out of the raw materials inside.  But you can't just rip into the package willy-nilly.  You have to ask for what you want, by name. "Give me the snod." "OK, now give me the lerd."  You unpack the contents, one by one, and make out of them whatever you're directed to make by the class and id.
  10.  
  11. The catch is that the package doesn't come with a packing list.  You're supposed to know what's in it -- you have a big book that tells you that whenever you get a package that you're supposed to make into a "wild plum", it's supposed to have a "bork", a "flam", and a "nitz" inside of it.  So you unpack the bork, the flam, and the nitz, and then you make the wild plum out of them and send it back.
  12.  
  13.     Apple event = package
  14.     class and id = terse directions for what to do with the package
  15.     parameters = contents of the package
  16.     keywords = names for each thing in the package
  17.  
  18. The lesson
  19. In HyperTalk, when you handle the appleEvent message, you first look at the class and id, and they tell you what you're supposed to do with the event.  If it's an event that you know how to handle, you already know what the parameters are -- you've agreed with the sender in advance on what's supposed to be in it, or it's a standard Apple event defined in the voluminous standard documentation.  So you unpack the parameters with the "request" command, manipulate them, and send back what you made out of them with the "reply" command.
  20.  
  21. The script might look like this:
  22.  
  23.     on appleEvent class,id,sender
  24.       if class & id is "wildplum" then
  25.         request ae data with keyword "bork"
  26.         if the result is not empty then   -- probably "Not found"
  27.           reply error "Expected a bork but got none."
  28.           exit appleEvent
  29.         end if
  30.         put it into theBork
  31.         request ae data with keyword "flam"
  32.         .
  33.         .
  34.         get wildPlumMakingFunction(theBork,theFlam,theNitz) -- process it
  35.         reply it  -- send back the reply
  36.       else pass appleEvent  -- we don't recognize this event; pass it on
  37.     end appleEvent
  38.  
  39. Keywords
  40. Think of a keyword for a piece of data in the package as its name -- it's the handle you need to pull it out of the package.  Asking for a parameter by keyword is the same as asking for a piece of raw material by name.
  41.  
  42. Keywords are arbitrary.  Whoever invents an Apple event makes them up, on the spot, out of the blue, to name the pieces of data that go along with the event.  The standard Apple events that have already been defined, along with the keywords for their required and optional parameters, are described in the Apple Event Registry.
  43.  
  44. The direct object
  45. Many Apple events arrive with just one piece of raw material in them, or one piece that's more important than the others.  By convention, this piece is given the keyword "----" and is referred to in the Apple event documenation as "the direct object" or "the direct parameter".  These are pretty ugly names, so in HyperTalk you can just call it the "data" and leave off the keyword specification -- HyperCard will know what you're talking about.
  46.  
  47.     request appleEvent data
  48.  
  49. This is equivalent to the following statement.
  50.  
  51.     request appleEvent data of keyword "----"
  52.  
  53. The direct object for the 'odoc' event is the list of documents to open.
  54. The direct object for the 'dosc' event is the script to execute.  And so on.
  55.  
  56. Event attributes
  57. Apple events come along with more than just an address, a verb, and pieces of data.  They also come with modifiers, or "attributes", which you can think of as "special handling instructions".  The class, id, and sender are all attributes also, but HyperCard gives you these automatically as parameters to the appleEvent message.  The timeout value, the transaction id, and the return id -- the identifier you put on the reply so that the sender knows, when his wild plum arrives, which package you made it out of -- are additional attributes.  If you need to look at these, the "request" command will let you.
  58.  
  59. You can get the return id,
  60.  
  61.     request appleEvent return id
  62.     request appleEvent data of keyword "rtid" -- same thing, only uglier
  63.  
  64. the timeout value,
  65.  
  66.     request appleEvent data of keyword "timo"
  67.  
  68. the transaction id,
  69.  
  70.     request appleEvent data of keyword "tran"
  71.  
  72. and whatever else you think you need, as long as you know its keyword.  We have special forms of the request command for the class, id, sender, direct object, and return id -- for everything else, you have to tell HyperCard what you want by keyword.
  73.  
  74.     request appleEvent class    -- gives you the class attribute
  75.     request appleEvent id    -- gives you the id attribute
  76.     request appleEvent sender    -- gives you the sender attribute
  77.     request appleEvent data    -- gives you the direct object parameter
  78.     request appleEvent return id    -- gives you the return id attribute
  79.